package org.masterinftel.scrumpmt.activities;

import java.util.ArrayList;
import java.util.List;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.R;
import org.masterinftel.scrumpmt.ScrumApplication;
import org.masterinftel.scrumpmt.adapters.ViewPagerAdapterColab;
import org.masterinftel.scrumpmt.json.model.ColaboradorJSON;
import org.masterinftel.scrumpmt.json.model.DualListColabJSON;
import org.masterinftel.scrumpmt.json.model.TaskColab;
import org.masterinftel.scrumpmt.protocol.Protocol;
import org.masterinftel.scrumpmt.util.UtilNetwork;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.google.gson.stream.JsonReader;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;

public class ColabActivity extends Activity {

	private ViewPagerAdapterColab mAdapter;
	private ViewPager mPager;
	private long productID;
	private ScrumApplication application;
    private Context ctx;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maincolab);
		 setTitle("ScrumPMT - Colaboradores");
		productID = getIntent().getLongExtra("position", 99999999);
		
		// creamos variables utiles para la aplicacion
        ctx = this;
        application = (ScrumApplication) getApplication();

		// ViewPagerAdapterColab adapter = new ViewPagerAdapterColab(this);
		mAdapter = new ViewPagerAdapterColab(this);
		mPager = (ViewPager) findViewById(R.id.viewpager);
		TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
		// pager.setAdapter(adapter);
		mPager.setAdapter(mAdapter);
		indicator.setViewPager(mPager);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.blackboard_menucolab, menu);
		if(!application.isAdmin())
		menu.removeItem(R.id.newcollaborator);
		return true;
	}

	private void changeTask(ArrayAdapter<TaskColab> from, ArrayAdapter<TaskColab> to) {
		ArrayList<TaskColab> taskToRemove = new ArrayList<TaskColab>();
		TaskColab taskColab;

		for (int i = 0; i < from.getCount(); i++) {
			taskColab = from.getItem(i);
			if (taskColab.isSelected()) {
				taskColab.setSelected(false);
				to.add(taskColab);
				taskToRemove.add(taskColab);
			}
		}

		// Eliminamos de la lista origen las tareas que se han movido
		for (TaskColab t : taskToRemove) {
			from.remove(t);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        String URLFrontController = prefs.getString(Constants.KEY_URL_PREFERENCE, "");
	    
	    String urlRest = URLFrontController + ";jsessionid="
                + application.getSessionID() + "?action="
                + Protocol.EDIT_COLAB + "&value=" + productID;

        DualListColabJSON list = new DualListColabJSON();
        List<TaskColab> lNoAsoc = new ArrayList<TaskColab>();
        List<TaskColab> lAsoc = new ArrayList<TaskColab>();
        
	    switch (item.getItemId()) {
		case R.id.menuColab:
			// Pasamos las tareas marcadas en DOING y DONE a TODO
			changeTask(mAdapter.getmListDOING(), mAdapter.getmListTODO());
			// changeTask(mAdapter.getmListDONE(), mAdapter.getmListTODO());
			mAdapter.instantiateItem(mPager, Constants.PAGE_CANDIDATOS);			
	        break;
		case R.id.menuAsoci:
			// Pasamos las tareas marcadas en TODO y DONE a DOING
			changeTask(mAdapter.getmListTODO(), mAdapter.getmListDOING());
			// changeTask(mAdapter.getmListDOING(), mAdapter.getmListTODO());
			mAdapter.instantiateItem(mPager, Constants.PAGE_ASOCIADOS);
			break;
		case R.id.newcollaborator:
			startActivity(new Intent(ctx,
                    NewUserActivity.class));
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
	    
	    for (int i = 0; i < mAdapter.getmListTODO().getCount(); i++) {
            lNoAsoc.add(mAdapter.getmListTODO().getItem(i));
        }
        for (int i = 0; i < mAdapter.getmListDOING().getCount(); i++) {
            lAsoc.add(mAdapter.getmListDOING().getItem(i));
        }
        list.setColabAsociados(lAsoc);
        list.setColabNoAsociados(lNoAsoc);

        JsonReader reader = UtilNetwork.insertJSON(urlRest, list);  
	    return true;
	}

}
